package cn.nju.seg.atg.GA;

public class CharsChromosome extends Chromosome{
	/** 染色体中的基因序列 */
    protected char[] genes;

    /**
     * 代参构造函数
     * @param iGenesDim
     */
    public CharsChromosome(int iGenesDim)
    {
        genes = new char[iGenesDim];
    }

    /**
     * toString
     * @return String
     */
    public String toString()
    {
        return (getGenesAsStr());
    }

    /**
     * 确定给定染色体与当前染色体中相同字符的个数
     * @param chromosome
     * @return int
     */
    public int getNumGenesInCommon(Chromosome chromosome)
    {
        int numGenesInCommon = 0;
        String chromGenes = chromosome.getGenesAsStr();

        for (int i = 0; i < genes.length; i++)
            if (this.genes[i] == chromGenes.charAt(i))
                numGenesInCommon++;
        return (numGenesInCommon);
    }

    /**
     * 将染色体中的基因串以char数组的形式返回
     * @return char[]
     */
    public char[] getGenes()
    {
        return(genes);
    }
    
    /**
     * 将染色体中的基因串以String类型返回
     */
    public String getGenesAsStr()
    {
        String sGenes = "";

        for (int i = 0; i < genes.length; i++)
            sGenes += genes[i];
        return (sGenes);
    }

    /** 返回染色体基因串中的某个基因
     *  @return char gene  
     */
    public char getGene(int iGene)
    {
        return (this.genes[iGene]);
    }

    /**
     * 将当前染色体的基因串设置为给定的String类型字符串
     * @param String 
     */
    public void setGenesFromStr(String sChromosome)
    {
        for (int i = 0; i < genes.length; i++)
            this.genes[i] = sChromosome.charAt(i);
    }

    /**
     * 拷贝给定染色体的基因串
     * @param Chromosome  
    */
    public void copyChromGenes(Chromosome chromosome)
    {
        int iGene;

        CharsChromosome ChromChars = (CharsChromosome)chromosome;
        for (iGene = 0; iGene < genes.length; iGene++)
            this.genes[iGene] = ChromChars.genes[iGene];
    }
}
